﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using TrackIT.Common;

namespace TrackIT.UnitTest
{
    [ExcludeFromCodeCoverage]
    public static class HelperUtil
    {
        #region Work list strings

        internal const string workListValid1 = 
@"% 2012-04-19
% EOL_Hardening 09:45 17:00
% 2012-04-23
% EOL_61579 10:14 11:22
% EOL_61703 11:22 13:17";
        public static List<string> WorkListValid1
        {
            get
            {
                return WorkEntryStringToList(workListValid1);
            }
        }

        internal const string workListMultipleTasks = 
@"% 2012-04-19
% EOL_Hardening 09:45 17:00
% 2012-04-23
% EOL_61579 10:14 11:22
% EOL_61703 11:22 13:17
% EOL_61579 13:17 13:56
% EOL_61703 13:56 17:06";
        public static List<string> WorkListMultipleTasks
        {
            get
            {
                return WorkEntryStringToList(workListMultipleTasks);
            }
        }

        internal const string workListLongList =
@"% 2012-03-12
% EOL_Meeting 06:40 17:45
% 2012-03-13
% EOL_Meeting 08:15 09:00
% EOL_59069 10:30 17:15
% 2012-03-14
% EOL_59197 10:15 17:15
% 2012-03-15
% EOL_0059197 08:30 09:30
% EOL_Reviews 10:00 17:30
% 2012-03-16
% EOL_60048 09:00 17:00
% 2012-03-19
% EOL_Reviews 09:00 11:00
% EOL_Meeting 11:00 16:00
% 2012-03-20
% EOL_Reviews 08:30 12:00
% EOL_59930 12:15 13:48
% EOL_OTRS 13:48 13:58
% EOL_59930 13:58 16:00
% EOL_60216 16:00 16:10
% EOL_TeamSupport 16:10 16:18
% EOL_59913 16:19 16:40
% 2012-03-21
% EOL_59930 08:35 09:30
% EOL_Standup 09:30 10:00
% EOL_59930 10:00 10:15
% EOL_Standup 10:15 10:40
% EOL_59836 10:48 11:17
% EOL_59930 11:17 13:40
% EOL_60091 15:02 16:00
% EOL_Meeting 15:02 16:00
% 2012-03-22
% EOL_59933 09:44 10:15
% EOL_59933 10:40 12:00
% EOL_59933 12:10 17:15
% 2012-03-23
% EOL_60428 09:44 17:00
% 2012-03-26
% EOL_60479 08:30 10:15
% EOL_60479 10:30 12:15
% EOL_59931 12:15 17:00
% 2012-03-27
% EOL_Meeting 09:30 09:45
% EOL_59933 10:30 12:00
% EOL_59933 12:30 17:30
% 2012-03-28
% EOL_Meeting 09:30 09:45
% EOL_Hardening 10:30 14:00
% EOL_Meeting 14:00 14:30
% EOL_Hardening 14:30 15:30
% EOL_Meeting 15:30 17:10
% 2012-03-29
% EOL_Meeting 08:30 09:45
% EOL_Hardening 09:45 17:30
% 2012-04-02
% EOL_Meeting 09:00 12:20
% EOL_Hardening 13:20 17:45
% 2012-04-03
% EOL_Meeting 09:30 09:45
% EOL_Hardening 09:45 13:00
% EOL_Selenium 13:15 17:00
% 2012-04-04
% EOL_Meeting 09:30 09:45
% EOL_Meeting 10:15 10:30
% EOL_Selenium 10:30 11:00
% EOL_60295 11:00 13:30
% EOL_60190 13:45 17:00
% 2012-04-05
% EOL_60190 08:45 09:30
% EOL_Meeting 09:30 09:45
% EOL_Meeting 10:15 10:30
% EOL_60190 10:30 15:35
% EOL_REVIEW 15:35 16:12
% EOL_60190 16:35 16:52
% EOL_REVIEW 16:52 17:10
% 2012-04-06
% EOL_Testing 08:45 09:30
% EOL_Meeting 09:30 10:30
% EOL_Testing 10:35 11:00
% EOL_Verification 11:00 13:00
% EOL_Review 13:00 16:00
% 2012-04-10
% EOL_60192 08:58 10:10
% EOL_REVIEW 10:10 10:57
% EOL_52875 10:57 17:00
% 2012-04-11
% EOL_52875 08:20 10:00
% EOL_Meeting 10:15 10:30
% EOL_REVIEW 10:30 11:50
% EOL_52875 11:50 10:49
% 2012-04-12
% EOL_52875 08:30 10:15
% EOL_T3 10:39 10:50
% EOL_52875 10:50 12:00
% EOL_T3 12:00 12:15
% EOL_52875 12:30 17:10";
        public static List<string> WorkListLongList
        {
            get
            {
                return WorkEntryStringToList(workListLongList);
            }
        }

        internal const string workListStartedTask1 = "% 2011-3-2\n\r% EOL_StartTask 13:56";
        public static List<string> WorkListStartedTask1
        {
            get
            {
                return WorkEntryStringToList(workListStartedTask1);
            }
        }

        internal const string workListStartedTask2 = "% 2012-11-20\n\r% EOL_12345 13:56";
        public static List<string> WorkListStartedTask2
        {
            get
            {
                return WorkEntryStringToList(workListStartedTask2);
            }
        }   
        #endregion

        public static List<string> WorkEntryStringToList(string taskList)
        {
            return taskList.Split('\n', '\r')
                .Where(l => !string.IsNullOrWhiteSpace(l))
                .ToList();
        }

        public static string ExtractPath(string filename)
        {
            int idx = filename.LastIndexOf("\\");
            if (idx > 0)
            {
                return filename.Substring(0, idx + 1);
            }
            else
            {
                throw new ArgumentException("The filename is invalid.");
            }
        }

        public static string StripWhiteSpace(string text)
        {
            return Regex.Replace(text, @"\s+", " ");
        }

        public static IList<WorkEntry> WorkEntryStringsToEntities(IList<string> workEntries)
        {
            return workEntries.Select(w => new WorkEntry(w)).ToList();
        }

        public static IList<Task> WorkEntryEntitiesToTaskEntities(IList<WorkEntry> workEntries)
        {
            return workEntries
                .Where(w => !string.IsNullOrWhiteSpace(w.Task))
                .Select(w => new Task(w.Task))
                .ToList();
        }

        public static bool IsTimeWithinMinutes(DateTime time1, DateTime time2, int minutes)
        {
            var timeSpan = (time1.TimeOfDay - time2.TimeOfDay);

            return Math.Abs(timeSpan.TotalMinutes) <= minutes;
        }
    }
}
